//
//  SPSettingViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/4/7.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPSettingViewController.h"
#import "SPModifyPasswordViewController.h"
#import "SPLoginViewController.h"

@interface SPSettingViewController(){
    UITableView * m_tableView;
    NSMutableArray * m_titleArray;
}
@end

@implementation SPSettingViewController

- (void)viewDidLoad{
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NSArray * accountArray = [[NSArray alloc] initWithObjects:@"账号管理",@"个人资料",@"银行卡绑定",@"实名认证",@"修改密码", nil];
    NSArray * systemArray = [[NSArray alloc] initWithObjects:@"系统设置",@"系统设置", nil];
    NSArray * otherArray = [[NSArray alloc] initWithObjects:@"意见反馈",@"关于超级合伙人", nil];
    NSArray * logoutArray = [[NSArray alloc] initWithObjects:@"退出登录", nil];

    CGRect frame = self.view.frame;
    frame.size.height -= 88;
    m_titleArray = [[NSMutableArray alloc]initWithObjects:accountArray,systemArray,otherArray,logoutArray, nil];
    
    m_tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStyleGrouped];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    [self.view addSubview:m_tableView];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [m_titleArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    
    return [[m_titleArray objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25.0f;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,25)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14];
    if (section == 0) {
        label.text = @"账号管理";
    }
    else if (section == 1){
        label.text = @"系统设置";
    }
    else if (section == 2){
        label.text = @"其他";
    }
    [headerView addSubview:label];
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    [cell setText:[[m_titleArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * title = [[m_titleArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if ([title isEqualToString:@"修改密码"]) {
        SPModifyPasswordViewController * vc = [[SPModifyPasswordViewController alloc]init];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
        nav.navigationBarHidden = YES;
        [self.navigationController presentViewController:nav animated:YES completion:nil];
    }
    else if ([title isEqualToString:@"退出登录"]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否退出登录?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alert addButtonWithTitle:@"退出"];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    //退出登录
    if (buttonIndex == 1) {
        SPLoginViewController * vc = [[SPLoginViewController alloc]init];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
        nav.navigationBarHidden = YES;
        [self.navigationController presentViewController:nav animated:NO completion:nil];
    }
}

@end
